<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Configuration Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage Configuration  
 * @author Manh Nguyen - 08/08/2012 
 * @link http://sunsoft.vn
 */
class Giftcode_model extends DataMapper {

    var $model = 'giftcode_model';
    var $table = 'giftcode';

    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    public function __construct() {
        parent::__construct();
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    var $has_one = array();
    var $has_many = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------
    var $validation = array();
    // --------------------------------------------------------------------

    /**
     * Default order in query select 
     */
    var $default_order_by = array('id');

    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->title) ? '---empty---' : $this->title;
    }
    
    public function get_exist_code($netcafe_id, $username) {
    	$giftcode = new Giftcode_model();
    	$giftcode->select('code');
    	$giftcode->where('office_id', $netcafe_id);
    	$giftcode->where('type', 'user');
    	$giftcode->where('username', $username);
    	$giftcode->get(1);
    	return $giftcode;
    }

    public function get_active_code($netcafe_id, $username) {   	
    	$giftcode = $this->get_exist_code($netcafe_id, $username);
    	
    	if(!$giftcode->exists()) {    	
    		$giftcode->select('id, code');
    		$giftcode->where('office_id', $netcafe_id);
    		$giftcode->where('type', 'user');
    		$giftcode->where('status', 0);    		
    		$giftcode->get(1);
    		
    		$now = date("Y:m:d H:i:s");
    		 
    		if($giftcode->exists()) {
    			$giftcode->get_by_id($giftcode->id);
    			$giftcode->status = 1;
    			$giftcode->username = $username;
    			$giftcode->created = $now;
    			$giftcode->save(); 
    		} 
    	}
    	return $giftcode;
    }
    
    public function count_active_code($netcafe_code) {
    	$giftcode = new Giftcode_model();
    	$giftcode->select('id');
    	$giftcode->where('type', 'netcafe');
    	$giftcode->get_by_code($netcafe_code);
    	 
    	$netcafe_id = $giftcode->id;
    	$giftcode->where('office_id', $netcafe_id);
    	$giftcode->where('type', 'user');
    	$giftcode->where('status', 0);    	
    	return $giftcode->count();
    }
    
    public function count_total_code($netcafe_code) {
    	$giftcode = new Giftcode_model();
    	$giftcode->select('id');
    	$giftcode->where('type', 'netcafe');
    	$giftcode->get_by_code($netcafe_code);
    
    	$netcafe_id = $giftcode->id;
    	$giftcode->where('office_id', $netcafe_id);
    	$giftcode->where('type', 'user');
    	return $giftcode->count();
    }

	public function extract_vipcode($user_name){
		$vipcode_by_user = new Giftcode_model();
		$vipcode_by_user->select("id, code");
		$vipcode_by_user->where("username", $user_name);
		$vipcode_by_user->where("type", 'vipcode');
		$vipcode_by_user->get(1);
		if($vipcode_by_user->id > 0){
			return strtoupper($vipcode_by_user->code);
		}else{
			$vipcode = new Giftcode_model();
			$vipcode->select("id, code");
			$vipcode->where("type", 'vipcode');
			$vipcode->group_start()
				->where("username")
				->or_where("username", "");
			$vipcode->group_end();
			$vipcode->get(1);
			
			//$vipcode->check_last_query();
			//exit;
			if($vipcode->id>0){
				$vipcode->username = $user_name;
				$vipcode->created = date("Y-m-d H:i:s");
				$vipcode->save(); 
				return strtoupper($vipcode->code);
			}else{
				return "expired";
			}
		}
	}
}

/* End of file configuration.php */
/* Location: ./application/models/configuration.php */